package com.github.chxiaowu.service;

import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.chxiaowu.mongo.MongoHelper;
/**
 * 查询公共方法
 * @author chenhua
 *
 */
@Service
public abstract class BaseService {

	static final String ID = "_id";

	static final String META_STATUS = "meta.status";

	@Autowired
	protected MongoHelper mongoHelper;

	protected abstract String collName();

	/**
	 * 根据条件获取一条记录,公共方法,没有POJO转换
	 * 
	 * @param filter
	 * @return
	 */
	public Document get(Bson filter) {
		return mongoHelper.getCollection(collName()).find(filter).first();
	}

	/**
	 * 根据条件获取一条记录,公共方法,没有POJO转换
	 * 
	 * @param filter
	 * @param projection
	 * @return
	 */
	public Document get(Bson filter, Bson projection) {
		return mongoHelper.getCollection(collName()).find(filter).projection(projection).first();
	}

	/**
	 * 根据条件更新一条记录,公共方法
	 * 
	 * @param filter
	 * @param update
	 * @return
	 */
	public boolean updateOne(Document filter, Document update) {
		return mongoHelper.getCollection(collName()).updateOne(filter, update).getMatchedCount() > 0;
	}

}
